#include "netsetwindow.h"
#include "ui_netsetwindow.h"

Netsetwindow::Netsetwindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Netsetwindow)
{
    ui->setupUi(this);
    initWindow();
    udp_socket = new QUdpSocket;
}

Netsetwindow::~Netsetwindow()
{
    delete ui;
}

void Netsetwindow::initWindow()
{
    QString name = QHostInfo::localHostName();
    QHostInfo hinfo= QHostInfo::fromName(name);
    QList<QHostAddress> addrs = hinfo.addresses();
    for(auto  addr : addrs)
    {
        if(addr.protocol() == QAbstractSocket::IPv4Protocol)
        {
            ui->ipComboBox->addItem(addr.toString());
        }
    }
}

void Netsetwindow::on_conButton_clicked()
{
#if 0
    /* udp*/
    udp_socket->bind(QHostAddress(ui->ipComboBox->currentText()),
                     ui->portLineEdit->text().toShort());
    connect(udp_socket,&QUdpSocket::readyRead,[=](){
        QByteArray arr;
        int size = udp_socket->pendingDatagramSize();
        arr.resize(size);
        QHostAddress addr;
        quint16 port;
        udp_socket->readDatagram(arr.data(),size,&addr,&port);
        ui->textEdit->append(arr);
    });
#endif
#if 0
    /*
     *  客户端
     */
    //连接远程主机
    if(ui->conButton->text() == "连接"){
        client_socket = new QTcpSocket(this);

        client_socket->connectToHost(QHostAddress(ui->ipComboBox->currentText()),
                                     ui->portLineEdit->text().toShort());

        connect(client_socket,&QTcpSocket::connected,[=](){
            ui->textEdit->append("连接已经建立");
            ui->conButton->setText("断开连接");
        });
    }
    else if(ui->conButton->text() == "断开连接")
    {
        client_socket->disconnectFromHost();
        ui->textEdit->append("连接已断开");
        client_socket->deleteLater();
        client_socket=nullptr;
        ui->conButton->setText("连接");
    }
#endif

    /*
 * 服务器
 */
#if 1
    if(ui->conButton->text() == "连接")
    {
        s_socket = new QTcpServer(this);

        bool isListen = s_socket->listen(QHostAddress(ui->ipComboBox->currentText()),
                                         ui->portLineEdit->text().toShort());
        if(isListen)
        {
            ui->textEdit->append("监听成功");
            ui->conButton->setText("关闭");
            connect(s_socket,&QTcpServer::newConnection,[=](){
                //获取通信套接字
                c_socket = s_socket->nextPendingConnection();
                connect(c_socket,&QTcpSocket::readyRead,[=](){
                    ui->textEdit->append( c_socket->readAll());
                });
                connect(c_socket,&QTcpSocket::disconnected,[=](){
                    ui->textEdit->append("["+c_socket->peerAddress().toString()+
                                         ":"+
                                         QString::number(c_socket->peerPort())+
                                         "] 连接已经断开");
                });
            });
        }
        else{
            ui->textEdit->append("监听失败");
        }
    }
    else if(ui->conButton->text() == "关闭")
    {
        //关闭监听
        s_socket->close();
        ui->conButton->setText("连接");
        ui->textEdit->append("监听关闭");
    }
#endif
}

void Netsetwindow::on_sendButton_clicked()
{
#if 0
    udp_socket->writeDatagram(ui->sendEdit->toPlainText().toUtf8(),
                              QHostAddress("127.0.0.1"),10001);
#endif
#if 1
    /* client*/
    if(client_socket)
        client_socket->write(ui->sendEdit->toPlainText().toUtf8());
#endif
}
